# MQTT客户端 Makefile
# 支持本地编译和嵌入式交叉编译

# 编译器配置
CC ?= gcc
CROSS_COMPILE ?=

# 当设置了交叉编译器时使用
ifneq ($(CROSS_COMPILE),)
    CC = $(CROSS_COMPILE)gcc
    AR = $(CROSS_COMPILE)ar
    STRIP = $(CROSS_COMPILE)strip
endif

# 第三方库路径配置
THIRD_PARTY_ROOT = ../../3party_lib

# 根据编译器选择对应的mosquitto库路径
ifeq ($(CROSS_COMPILE),riscv64-anyka-linux-musl-)
    MOSQUITTO_ROOT = $(THIRD_PARTY_ROOT)/mosquitto/install/riscv64
else ifeq ($(CROSS_COMPILE),arm-nextvpu-linux-gnueabihf-)
    MOSQUITTO_ROOT = $(THIRD_PARTY_ROOT)/mosquitto/install/arm
else
    MOSQUITTO_ROOT = $(THIRD_PARTY_ROOT)/mosquitto/install/gcc
endif

# 编译选项
CFLAGS = -Wall -Wextra -std=c99 -O2
CFLAGS += -D_GNU_SOURCE

# 添加mosquitto头文件路径
CFLAGS += -I$(MOSQUITTO_ROOT)/include

# 调试版本
ifeq ($(DEBUG),1)
    CFLAGS += -g -DDEBUG
else
    CFLAGS += -DNDEBUG
endif

# 嵌入式版本
ifeq ($(EMBEDDED),1)
    CFLAGS += -DEMBEDDED_SYSTEM
endif

# 库文件
LIBS = -lmosquitto -lpthread
LDFLAGS += -L$(MOSQUITTO_ROOT)/lib

# 静态链接选项(适用于嵌入式系统)
ifeq ($(STATIC),1)
    LDFLAGS += -static
    LIBS = -L$(MOSQUITTO_ROOT)/lib -lmosquitto -lpthread -lssl -lcrypto -lc-ares -ldl
else
    LIBS = -L$(MOSQUITTO_ROOT)/lib -lmosquitto -lpthread
endif

# 目标文件
TARGET = mqtt_client
TARGET_STATIC = mqtt_client_static
TARGET_TEST = mqtt_test_simple
TARGET_SYNC = mqtt_client_sync

# 源文件
SRCS = mqtt_client.c
TEST_SRCS = mqtt_test_simple.c
SYNC_SRCS = mqtt_client_sync.c
HEADERS = mqtt_client.h mqtt_config.h
OBJS = $(SRCS:.c=.o)
TEST_OBJS = $(TEST_SRCS:.c=.o)
SYNC_OBJS = $(SYNC_SRCS:.c=.o)

# 默认目标
all: $(TARGET)

# 主目标
$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(LDFLAGS)

# 简单测试版本
test: $(TARGET_TEST)

$(TARGET_TEST): $(TEST_OBJS)
	$(CC) $(CFLAGS) -o $@ $^ -lmosquitto

# 同步版本（修复连接问题）
sync: $(TARGET_SYNC)

$(TARGET_SYNC): $(SYNC_OBJS)
	$(CC) $(CFLAGS) -o $@ $^ -lmosquitto -lpthread

# 静态链接版本
static: $(TARGET_STATIC)

$(TARGET_STATIC): $(OBJS)
	$(CC) $(CFLAGS) -static -o $@ $^ $(LIBS) $(LDFLAGS)

# 编译对象文件
%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 测试对象文件
mqtt_test_simple.o: mqtt_test_simple.c
	$(CC) $(CFLAGS) -c -o $@ $<

# 调试对象文件
mqtt_debug.o: mqtt_debug.c
	$(CC) $(CFLAGS) -c -o $@ $<

# 嵌入式版本
embedded:
	$(MAKE) EMBEDDED=1 STATIC=1

# ARM交叉编译
arm:
	$(MAKE) CROSS_COMPILE=arm-linux-gnueabihf- EMBEDDED=1

# RISC-V交叉编译
riscv64: mqtt_client_riscv64

mqtt_client_riscv64:
	$(MAKE) CROSS_COMPILE=riscv64-anyka-linux-musl- EMBEDDED=1 TARGET=mqtt_client_riscv64

# 调试版本
debug:
	$(MAKE) DEBUG=1

# 调试测试版本
test-debug:
	$(MAKE) test DEBUG=1

# 运行网络测试
test-network:
	@chmod +x test_network.sh
	./test_network.sh

# 运行本地测试环境
test-local:
	@chmod +x test_local.sh
	./test_local.sh

# 快速测试 - 编译并运行简单版本
quick-test: test
	@echo "=== 运行简单MQTT测试 ==="
	@echo "提示: 按Ctrl+C退出"
	@echo "在另一个终端运行以下命令进行测试:"
	@echo "  mosquitto_sub -h test.mosquitto.org -t 'test/simple'"
	@echo "  mosquitto_pub -h test.mosquitto.org -t 'test/simple' -m '外部测试消息'"
	@echo ""
	./$(TARGET_TEST)

# 安装
install: $(TARGET)
	cp $(TARGET) /usr/local/bin/
	cp $(HEADERS) /usr/local/include/

# 清理
clean:
	rm -f $(TARGET) $(TARGET_STATIC) $(TARGET_TEST) $(TARGET_DEBUG) $(OBJS) $(TEST_OBJS) $(DEBUG_OBJS) *.o

# 深度清理
distclean: clean
	rm -f *~ *.bak

# 依赖库检查
check-deps:
	@echo "检查mosquitto库..."
	@if [ -f "$(MOSQUITTO_ROOT)/include/mosquitto.h" ]; then \
		echo "mosquitto 头文件: ✅ $(MOSQUITTO_ROOT)/include/mosquitto.h"; \
	else \
		echo "mosquitto 头文件: ❌ 未找到"; \
		echo "请运行: cd $(THIRD_PARTY_ROOT)/mosquitto && ./install_mosquitto_*.sh"; \
	fi
	@if [ -f "$(MOSQUITTO_ROOT)/lib/libmosquitto.a" ]; then \
		echo "mosquitto 静态库: ✅ $(MOSQUITTO_ROOT)/lib/libmosquitto.a"; \
	else \
		echo "mosquitto 静态库: ❌ 未找到"; \
	fi
	@echo "检查pthread..."
	@echo '#include <pthread.h>' | $(CC) -x c - -lpthread -o /dev/null 2>/dev/null && echo "pthread: ✅" || echo "pthread: ❌"

# 显示帮助
help:
	@echo "可用目标:"
	@echo "  all           - 构建标准版本"
	@echo "  test          - 构建简单测试版本"
	@echo "  static        - 构建静态链接版本"
	@echo "  embedded      - 构建嵌入式版本(静态)"
	@echo "  arm           - ARM交叉编译"
	@echo "  riscv64       - RISC-V交叉编译"
	@echo "  debug         - 构建调试版本"
	@echo "  test-debug    - 构建调试测试版本"
	@echo "  quick-test    - 编译并运行简单测试"
	@echo "  test-network  - 运行网络连接测试"
	@echo "  test-local    - 设置本地测试环境"
	@echo "  install       - 安装到系统"
	@echo "  clean         - 清理构建文件"
	@echo "  check-deps    - 检查依赖库"
	@echo ""
	@echo "变量:"
	@echo "  DEBUG=1       - 启用调试构建"
	@echo "  EMBEDDED=1    - 启用嵌入式特性"
	@echo "  STATIC=1      - 启用静态链接"
	@echo "  CROSS_COMPILE - 设置交叉编译器前缀"
	@echo ""
	@echo "测试步骤:"
	@echo "  1. make check-deps    # 检查依赖"
	@echo "  2. 如果缺少mosquitto库，运行:"
	@echo "     cd $(THIRD_PARTY_ROOT)/mosquitto"
	@echo "     ./install_mosquitto_gcc.sh      # 本地GCC版本"
	@echo "     ./install_mosquitto_riscv64.sh  # RISC-V交叉编译版本"
	@echo "  3. make test-network  # 测试网络连接"
	@echo "  4. make quick-test    # 快速测试"
	@echo "  5. make test-local    # 本地环境测试"

.PHONY: all test static embedded arm riscv64 debug test-debug install clean distclean check-deps help test-network test-local quick-test
